import { injectable } from 'inversify';
import * as dotenv from 'dotenv';

@injectable()
export class ConfigService {
  private config: { [key: string]: string | undefined };

  constructor() {
    dotenv.config();
    this.config = process.env;
  }

  get(key: string): string | undefined {
    return this.config[key];
  }

  getWebsocketPort(): number {
    return parseInt(this.get('WEBSOCKET_PORT') || '8080', 10);
  }

  getDatabaseType(): string {
    return this.get('DATABASE_TYPE') || 'sqlite';
  }

  getDatabaseUrl(): string {
    return this.get('DATABASE_URL') || './database.sqlite';
  }

  getLogLevel(): string {
    return this.get('LOG_LEVEL') || 'info';
  }

  // 添加认证相关配置
  getJwtSecret(): string {
    return this.get('JWT_SECRET') || 'default-very-secret-key-change-in-production';
  }

  getAccessTokenExpiresIn(): number {
    return parseInt(this.get('ACCESS_TOKEN_EXPIRES') || '3600', 10); // 默认1小时
  }

  getRefreshTokenExpiresIn(): number {
    return parseInt(this.get('REFRESH_TOKEN_EXPIRES') || '604800', 10); // 默认7天
  }

  getCorsOrigins(): string[] {
    const origins = this.get('CORS_ORIGINS');
    return origins ? origins.split(',') : ['*'];
  }
}